home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap18 / Emf8 / Emf.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.9 KB  |  171 lines

  1. /*--------------------------------------------------------
  2.    EMF.C -- Enhanced Metafile Demonstration Shell Program
  3.             (c) Charles Petzold, 1998
  4.   --------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <commdlg.h>
  8. #include "..\\emf8\\resource.h"
  9.  
  10. extern void CreateRoutine (HWND) ;
  11. extern void PaintRoutine  (HWND, HDC, int, int) ;
  12.  
  13. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  14.  
  15. HANDLE hInst ;
  16.  
  17. extern TCHAR szClass [] ;
  18. extern TCHAR szTitle [] ;
  19.  
  20. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  21.                     PSTR szCmdLine, int iCmdShow)
  22. {
  23.      TCHAR    szResource [] = TEXT ("EMF") ;
  24.      HWND     hwnd ;
  25.      MSG      msg ;
  26.      WNDCLASS wndclass ;
  27.  
  28.      hInst = hInstance ;
  29.  
  30.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  31.      wndclass.lpfnWndProc   = WndProc ;
  32.      wndclass.cbClsExtra    = 0 ;
  33.      wndclass.cbWndExtra    = 0 ;
  34.      wndclass.hInstance     = hInstance ;
  35.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  36.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  37.      wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  38.      wndclass.lpszMenuName  = szResource ;
  39.      wndclass.lpszClassName = szClass ;
  40.      
  41.      if (!RegisterClass (&wndclass))
  42.      {
  43.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  44.                       szClass, MB_ICONERROR) ;
  45.           return 0 ;
  46.      }
  47.      
  48.      hwnd = CreateWindow (szClass, szTitle,
  49.                           WS_OVERLAPPEDWINDOW,
  50.                           CW_USEDEFAULT, CW_USEDEFAULT,
  51.                           CW_USEDEFAULT, CW_USEDEFAULT,
  52.                           NULL, NULL, hInstance, NULL) ;
  53.  
  54.      ShowWindow (hwnd, iCmdShow) ;
  55.      UpdateWindow (hwnd) ;
  56.  
  57.      while (GetMessage (&msg, NULL, 0, 0))
  58.      {
  59.           TranslateMessage (&msg) ;
  60.           DispatchMessage (&msg) ;
  61.      }
  62.      return msg.wParam ;
  63. }
  64.  
  65. BOOL PrintRoutine (HWND hwnd)
  66. {
  67.      static DOCINFO  di ;
  68.      static PRINTDLG printdlg = { sizeof (PRINTDLG) } ;
  69.      static TCHAR    szMessage [32] ;
  70.      BOOL            bSuccess = FALSE ;
  71.      HDC             hdcPrn ;
  72.      int             cxPage, cyPage ;
  73.  
  74.     printdlg.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION ;
  75.  
  76.     if (!PrintDlg (&printdlg))
  77.           return TRUE ;
  78.  
  79.      if (NULL == (hdcPrn = printdlg.hDC))
  80.           return FALSE ;
  81.  
  82.      cxPage = GetDeviceCaps (hdcPrn, HORZRES) ;
  83.      cyPage = GetDeviceCaps (hdcPrn, VERTRES) ;
  84.  
  85.      lstrcpy (szMessage, szClass) ;
  86.      lstrcat (szMessage, TEXT (": Printing")) ;
  87.  
  88.      di.cbSize      = sizeof (DOCINFO) ;
  89.      di.lpszDocName = szMessage ;
  90.  
  91.      if (StartDoc (hdcPrn, &di) > 0)
  92.      {
  93.           if (StartPage (hdcPrn) > 0)
  94.           {
  95.                PaintRoutine (hwnd, hdcPrn, cxPage, cyPage) ;
  96.  
  97.                if (EndPage (hdcPrn) > 0)
  98.                {
  99.                     EndDoc (hdcPrn) ;
  100.                     bSuccess = TRUE ;
  101.                }
  102.           }
  103.      }
  104.      DeleteDC (hdcPrn) ;
  105.  
  106.      return bSuccess ;
  107. }
  108.  
  109. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  110. {
  111.      BOOL        bSuccess ;
  112.      static int  cxClient, cyClient ;
  113.      HDC         hdc ;
  114.      PAINTSTRUCT ps ;
  115.      
  116.      switch (message)
  117.      {
  118.      case WM_CREATE:
  119.           CreateRoutine (hwnd) ;
  120.           return 0 ;
  121.           
  122.      case WM_COMMAND:
  123.           switch (wParam)
  124.           {
  125.           case IDM_PRINT:
  126.                SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  127.                ShowCursor (TRUE) ;
  128.                
  129.                bSuccess = PrintRoutine (hwnd) ;
  130.                
  131.                ShowCursor (FALSE) ;
  132.                SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  133.                
  134.                if (!bSuccess)
  135.                     MessageBox (hwnd,
  136.                                 TEXT ("Error encountered during printing"),
  137.                                 szClass, MB_ICONASTERISK | MB_OK) ;
  138.                return 0 ;
  139.                
  140.           case IDM_EXIT:
  141.                SendMessage (hwnd, WM_CLOSE, 0, 0) ;
  142.                return 0 ;
  143.                
  144.           case IDM_ABOUT:
  145.                MessageBox (hwnd, TEXT ("Enhanced Metafile Demo Program\n")
  146.                                  TEXT ("Copyright (c) Charles Petzold, 1998"),
  147.                            szClass, MB_ICONINFORMATION | MB_OK) ;
  148.                return 0 ;
  149.           }
  150.           break ;
  151.           
  152.      case WM_SIZE:
  153.           cxClient = LOWORD (lParam) ;
  154.           cyClient = HIWORD (lParam) ;
  155.           return 0 ;
  156.           
  157.      case WM_PAINT:
  158.           hdc = BeginPaint (hwnd, &ps) ;
  159.           
  160.           PaintRoutine (hwnd, hdc, cxClient, cyClient) ;
  161.           
  162.           EndPaint (hwnd, &ps) ;
  163.           return 0 ;
  164.           
  165.      case WM_DESTROY :
  166.           PostQuitMessage (0) ;
  167.           return 0 ;
  168.      }
  169.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  170. }
  171.